
using System;
using System.Linq;
using Csla;
using Csla.Data;

namespace HousingInfo.Library
{ 
	public partial class ContactBOList : Csla.BusinessListBase<ContactBOList, ContactBO>
	{

		#region Authorization Rules

		protected static void AddObjectAuthorizationRules()
		{
			//TODO: Define object-level authorization rules of ContactBOList
			//AuthorizationRules.AllowCreate(typeof(ContactBOList), "ContactBOListCreateGroup"));
			//AuthorizationRules.AllowEdit(typeof(ContactBOList), "ContactBOListEditGroup"));
			//AuthorizationRules.AllowDelete(typeof(ContactBOList), "ContactBOListDeleteGroup"));
		}
		#endregion //Authorization Rules

        #region Trang Admin - Ho Tro Lien He

        #region Ham Lay danh sach Ho Tro Hoac Lien He
        public static ContactBOList GetContactBOList_Value(bool isyh, bool issk, bool islh)
        {
            return DataPortal.Fetch<ContactBOList>(new FilterCriteriaGetCategory_Value(isyh, issk, islh));
        }
        #endregion //End ham lay tung loai danh muc

        #region khai bao tham so
        private class FilterCriteriaGetCategory_Value
        {
            public bool yahoo;
            public bool skyper;
            public bool lienhe;
            public FilterCriteriaGetCategory_Value(bool yh, bool sk, bool lh)
            {
                this.yahoo = yh;
                this.skyper = sk;
                this.lienhe = lh;
            }
        }
        #endregion//end khai bao tham so

        #region Truy cap du lieu
        private void DataPortal_Fetch(FilterCriteriaGetCategory_Value criteria)
        {
            bool cancel = false;
            OnFetching(ref cancel);
            if (cancel) return;

            using (var mgr = ContextManager<HousingInfo.DataLinQ.HousingInfoDataContext>
                        .GetManager(Database.HousingInfoConnection))
            {
                RaiseListChangedEvents = false;
                if (criteria.lienhe == true)
                {
                    this.AddRange(
                        from row in mgr.DataContext.Contacts
                        orderby row.CreadedDate descending
                        where row.Email != ""
                        select ContactBO.GetContactBO(row.ContactID)
                    );
                }
                else if (criteria.yahoo == true && criteria.skyper == true)
                {
                    this.AddRange(
                        from row in mgr.DataContext.Contacts
                        orderby row.CreadedDate descending
                        where row.Email == ""
                        select ContactBO.GetContactBO(row.ContactID)
                    );
                }
                else if (criteria.yahoo == true && criteria.skyper == false)
                {
                    this.AddRange(
                        from row in mgr.DataContext.Contacts
                        orderby row.CreadedDate descending
                        where row.IsYahoo != "" && row.Email == ""
                        select ContactBO.GetContactBO(row.ContactID)
                    );
                }
                else
                {
                    this.AddRange(
                        from row in mgr.DataContext.Contacts
                        orderby row.CreadedDate descending
                        where row.IsSkyper != "" && row.Email == ""
                        select ContactBO.GetContactBO(row.ContactID)
                    );
                }

                RaiseListChangedEvents = true;
            }//using


            //OnFetched();
        }
        #endregion//end truy cap

        #endregion //End Trang Admin Ho Tro Lien He
	}
}
